package com.amat0urheu.service.cloudService.impl;
/*
 *@Auther:amaT0ur
 *@create:2023-11-15
 */

import com.amat0urheu.dao.IpPortMapper;
import com.amat0urheu.dao.SceneMapper;
import com.amat0urheu.pojo.CloudPojo.IpPort;
import com.amat0urheu.pojo.CloudPojo.Scene;
import com.amat0urheu.pojo.ExperimentScene;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.service.cloudService.IpPortService;
import com.amat0urheu.service.cloudService.SceneService;
import com.amat0urheu.util.CommonUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SceneServiceImpl implements SceneService {

    @Autowired
    SceneMapper sceneMapper;
    @Override
    public ReturnRes getScenes(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Scene> scenes = sceneMapper.getScenes();

        if (scenes != null) {
            for (Scene s:scenes) {
               s.setVictimIds2(sceneMapper.getVictimIdsBysId(s.getId()));
            }

            return new ReturnRes(true, new PageInfo<>(scenes), "查询成功");
        }
        return new ReturnRes(false, "查询失败");
    }

    @Override
    public ReturnRes getExperimentScenes() {



        return  new ReturnRes(true, sceneMapper.getExperimentScenes());
    }

    @Override
    public ReturnRes saveScene(Scene scene) {
        if( sceneMapper.getSceneIdBySceneName(scene.getScene())!=null){
            return new ReturnRes(false, "该场景模板已存在，请勿重复插入");
        }
        sceneMapper.saveScene(scene);
        for(int i =0;i<scene.getVictimIds2().size();i++){
            sceneMapper.saveSceneIdAndImageId( sceneMapper.getSceneIdBySceneName(scene.getScene()) ,scene.getVictimIds2().get(i));
        }
          return new ReturnRes(false, "场景模板增加成功");
    }

    @Override
    public ReturnRes searchScenePage(String key, String value, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        String[] whiteList = new String[]{"scene"};
        if (CommonUtil.strArrayIsHave(whiteList, key)) {

            List<Scene>  sceneList = sceneMapper.searchScenePage(key, "%" + value + "%");

            if (sceneList != null) {
                return new ReturnRes(true, new PageInfo<>(sceneList), "查询成功");
            } else {
                return new ReturnRes(false, "查询失败");
            }
        } else {
            return new ReturnRes(false, "师傅请勿尝试不安全的参数");
        }
    }

    @Override
    public ReturnRes updateScene(Scene scene) {

        sceneMapper.updateScene(scene);
        sceneMapper.deleteImageIdofScene(sceneMapper.getSceneIdBySceneName(scene.getScene()));
        for(int i =0;i<scene.getVictimIds2().size();i++){
            sceneMapper.saveSceneIdAndImageId( sceneMapper.getSceneIdBySceneName(scene.getScene()) ,scene.getVictimIds2().get(i));
        }
        return new ReturnRes(true, "场景模板更新成功");

    }

    @Override
    public ReturnRes removeScene(Integer sId) {
        sceneMapper.removeScene(sId);

        return new ReturnRes(true, "删除成功");
    }
}
